"""
模拟乒乓球击打墙面

暂不考虑：
1. 变更小球方向的情况。

"""

import arcade
from arcade.clock import GLOBAL_CLOCK
import math
import random


WINDOW_WIDTH=200
WINDOW_HEIGHT=200
WINDOW_COLOR=arcade.color.BLACK

BOARD_WIDTH=50
BOARD_HEIGHT=10
BOARD_SPEED=2
BOARD_COLOR=arcade.color.WHITE

BALL_RADIUS=10
BALL_SPEED=2
BALL_COLOR=arcade.color.RED_BROWN


class Board:
    def __init__(self):
        self.x=WINDOW_WIDTH//2
        self.y=BOARD_HEIGHT//2
        self.direction_x=0

    def update(self):
        # direction_x表示方向。0 不动，1 向右移动，-1 向左移动
        if self.direction_x==0:
            return
        if self.direction_x==1:
            self.x+=BOARD_SPEED
        elif self.direction_x==-1:
            self.x-=BOARD_SPEED

        # 检测有没有到边界
        if self.x<=BOARD_WIDTH//2:
            self.x=BOARD_WIDTH//2
        if self.x>=WINDOW_WIDTH-BOARD_WIDTH//2:
            self.x=WINDOW_WIDTH-BOARD_WIDTH//2

    def draw(self):
        arcade.draw_rect_filled(arcade.rect.XYWH(self.x,self.y,BOARD_WIDTH,BOARD_HEIGHT),BOARD_COLOR)

class Ball:

    def __init__(self):
        self.x=WINDOW_WIDTH//2
        self.y=WINDOW_HEIGHT//2
        self.rad=random.uniform(0,math.pi*2)
        print(f'ball rad:{self.rad}')
        self.delta_x=math.sin(self.rad)*BALL_SPEED
        self.delta_y=math.cos(self.rad)*BALL_SPEED
        # self.game_over=False 外部判定

    def update(self,board:Board):
        # 如果出了窗口，则小球不再移动
        if self.y<-BALL_RADIUS:
            return


        # 判定是否碰撞，如果碰撞，则修改delta_x和delta_y的值
        if self.x<=BALL_RADIUS:
            self.delta_x=-self.delta_x
        if self.x>=WINDOW_WIDTH-BALL_RADIUS:
            self.delta_x=-self.delta_x
        if self.y>=WINDOW_HEIGHT-BALL_RADIUS:
            self.delta_y=-self.delta_y
        if self.y<=BOARD_HEIGHT+BALL_RADIUS and abs(self.x-board.x)<BOARD_WIDTH//2:
            self.delta_y=-self.delta_y

        # 如果小球到底部，则游戏结束（外部判定）
        # if self.y<=0:
        #     self.game_over=True
        

        # 根据delta_x和delta_y变更xy坐标
        self.x+=self.delta_x
        self.y+=self.delta_y

    def draw(self):
        arcade.draw_circle_filled(self.x,self.y,BALL_RADIUS,BALL_COLOR)
        pass



class GameView(arcade.View):

    def __init__(self):
        super().__init__()
        self.ball=None
        self.board=None
        self.start_frame=0

    def setup(self):
        self.ball=Ball()
        self.board=Board()
        self.start_frame=GLOBAL_CLOCK.ticks
        pass


    def on_update(self, delta_time):
        self.ball.update(self.board)
        self.board.update()
        # if GLOBAL_CLOCK.ticks_since(self.start_frame) % 4 == 0:
            
        #     pass
    
    def on_draw(self):
        self.clear()
        self.ball.draw()
        self.board.draw()

    def on_key_press(self, symbol, modifiers):
        if symbol==arcade.key.LEFT:
            self.board.direction_x=-1
        elif symbol==arcade.key.RIGHT:
            self.board.direction_x=1
        # elif symbol==arcade.key.DOWN or symbol==arcade.key.UP:
        #     self.board.direction_x=0
        pass

    def on_key_release(self, symbol, modifiers):
        # return super().on_key_release(symbol, modifiers)
        self.board.direction_x=0
    
def main():
    window=arcade.Window(WINDOW_WIDTH,WINDOW_HEIGHT)

    game_view=GameView()
    game_view.setup()

    window.show_view(game_view)

    arcade.run()

if __name__=='__main__':
    main()